// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var app = angular.module('starter', ['ionic', 'starter.config', 'starter.uirouter', 'starter.directive', 'starter.controllers', 'starter.modules', 'starter.services', 'base64', 'angular-md5', 'ion-datetime-picker', 'ngCordova','highcharts-ng','monospaced.elastic','textAngular'])
.run(['$ionicPlatform', '$rootScope', '$state', 'UserInfo','$ionicPickerI18n','$cordovaAppVersion','$BPHttp','ApiUrl','$BPPopup','$jpushService','$cordovaDevice','$http',
  function ($ionicPlatform, $rootScope, $state,UserInfo,$ionicPickerI18n,$cordovaAppVersion,$BPHttp,ApiUrl,$BPPopup,$jpushService,$cordovaDevice,$http) {
    $ionicPlatform.ready(function() {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);
          cordova.plugins.Keyboard.disableScroll(true);
      }
      /*判断当前是否有登录信息*/
      $rootScope.$on('$stateChangeStart',
      function (event, toState) {
          if (toState.name != 'login' && UserInfo.get().isLogin != true)
          {
            event.preventDefault();
            $state.go('login');
          }
      });
      if ($state.current.name != 'login')
      {
          if (UserInfo.get().isLogin != true)
          {
            $state.go('login');
          }
      }
	  if($jpushService.check()){
		  //启动极光推送服务
		  window.plugins.jPushPlugin.init();
		  if (UserInfo.get().isLogin != true)
		  {
			  $jpushService.setTagsWithAlias(null,null);
		  }
		  if($cordovaDevice.getPlatform()!="Android")
		  {
			  window.plugins.jPushPlugin.setBadge(0);
			  window.plugins.jPushPlugin.resetBadge();
			  window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
		  }
		  window.plugins.jPushPlugin.setDebugMode(true);
	  }
	    /*
		//推送初始化
		var setTagsWithAliasCallback=function(event){
		  //window.alert('result code:'+event.resultCode+' tags:'+event.tags+' alias:'+event.alias);
		}
		var openNotificationInAndroidCallback=function(data){
			  var json=data;
			  //window.alert(json);
			  if(typeof data === 'string'){
				json=JSON.parse(data);
			  }
			  if(json.extras.flowid){
				  var id=json.extras.flowid;
				  var alert = json.extras['cn.jpush.android.ALERT'];
				  $state.go('messageDetail',{flowId:id,msg:alert});
				  //$state.go('flowBefProcess',{flowId:id});
			  }
		}
		var config={
		  stac:setTagsWithAliasCallback,
		  oniac:openNotificationInAndroidCallback
		};

		$jpushService.init(config);

		//启动极光推送服务
		window.plugins.jPushPlugin.init();
		window.plugins.jPushPlugin.setDebugMode(true);
	  */
	  $http.get(ApiUrl.firimApi)
            .success(function (result) {
                var data=eval(result);
                if(data.version)
			   {
				   var serverMessage = data.changelog;//更新提示
				   var serverURL = data.update_url;//更新地址
				   var serverAppVersion = data.versionShort;//服务器 版本  
				   console.log(serverAppVersion);
				   $cordovaAppVersion.getVersionNumber().then(function(version) {
						if (version != serverAppVersion) {
							//提示下载
							$BPPopup.confirm({
								title: '<p> </p>'+serverMessage+'<p> </p>',
								okText: '更新',
								ok: function () {
									window.open(serverURL, '_system', 'location=no');
								}
							});
						}					
				   });
			   }
            })
            .error(function () {
               
      });
	  /*
	  $BPHttp.get({
		"url": ApiUrl.firimApi,
		"success": function (data) {
		   if(data.version)
		   {
			   var serverMessage = obj.changelog;//更新提示
			   var serverURL = data.update_url;//更新地址
			   var serverAppVersion = data.version;//服务器 版本  
			   $cordovaAppVersion.getVersionNumber().then(function(version) {
					if (version != serverAppVersion) {
						//提示下载
						$BPPopup.confirm({
							title: '<p> </p>'+serverMessage+'<p> </p>',
							okText: '更新',
							ok: function () {
								window.open(serverURL, '_system', 'location=no');
							}
						});
					}					
			   });
		   }
		  
		   if (data.status.code == 0) {
			   var obj=JSON.parse(data.status.desc);
			   var serverAppVersion = obj.AppVersion;//服务器 版本  
			   var serverMessage = obj.Message;//更新提示
			   var serverURL = obj.URL;//更新地址
			   $cordovaAppVersion.getVersionNumber().then(function(version) {
					if (version != serverAppVersion) {
						//提示下载
						$BPPopup.confirm({
							title: '<p> </p>'+serverMessage.replace(/(\r\n)|(\n)/g,'<br>')+'<p> </p>',
							okText: '更新',
							ok: function () {
								window.open(serverURL, '_system', 'location=no');
							}
						});
					}					
			   });
		   }
		},
		"error": function () {
			
		}
      });
	  */
    });
	
	$ionicPickerI18n.weekdays = ["S", "M", "T", "W", "T", "F", "S"];
    $ionicPickerI18n.months = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
    $ionicPickerI18n.ok = "确定";
    $ionicPickerI18n.cancel = "取消";	
	
}])
.config(['$ionicConfigProvider',function ($ionicConfigProvider) {
    $ionicConfigProvider.backButton.previousTitleText(false);
    $ionicConfigProvider.backButton.text('').icon('ion-ios-arrow-left');
    $ionicConfigProvider.scrolling.jsScrolling(true);
    $ionicConfigProvider.platform.ios.tabs.style('standard');
    $ionicConfigProvider.platform.ios.tabs.position('bottom');
    $ionicConfigProvider.platform.android.tabs.style('standard');
    $ionicConfigProvider.platform.android.tabs.position('bottom');
    $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
    $ionicConfigProvider.platform.android.navBar.alignTitle('center');
    $ionicConfigProvider.platform.ios.views.transition('ios');
    $ionicConfigProvider.platform.android.views.transition('android');
    $ionicConfigProvider.tabs.position('bottom'); // other values: top
}])
/*
.config(['ionicDatePickerProvider',function (ionicDatePickerProvider) {
    var datePickerObj = {
        inputDate: new Date(),
        setLabel: 'Set',
        todayLabel: 'Today',
        closeLabel: 'Close',
        mondayFirst: false,
        weeksList: ["S", "M", "T", "W", "T", "F", "S"],
        monthsList: ["Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"],
        templateType: 'popup',
        from: new Date(2014, 8, 1),
        to: new Date(2020, 8, 1),
        showTodayButton: true,
        dateFormat: 'yyyy-MMMM-dd',
        closeOnSelect: false,
        disableWeekdays: [6],
    };
    ionicDatePickerProvider.configDatePicker(datePickerObj);
}])
*/
;


